package com.cvte.sunkit.manage

import android.app.Service
import android.content.Intent
import android.content.res.Resources
import android.media.projection.MediaProjectionManager
import android.view.WindowManager
import com.cvte.sunkit.App
import com.cvte.sunkit.model.PerfData
import com.cvte.sunkit.service.FloatingService
import com.cvte.sunkit.ui.window.ChartFloatWindow
import com.cvte.sunkit.ui.window.MainFloatWindow
import kotlinx.coroutines.*


/**
 *  @PackageName com.cvte.sunkit.manage
 *  @Description 浮窗管理类，负责管理浮窗之间的事件和数据
 *  @Author lixu
 *  @Data 2023/7/21 11:44
 *  @Version 1.0
 */
class FloatWindowManager(
    private val service: FloatingService,
    windowManager: WindowManager,
    private val resources: Resources,
    private val code:Int,
    private val data:Intent
) {
    private lateinit var mRecordManager: RecordManager
    private lateinit var mAbnormalDataRecordManage: AbnormalDataRecordManager
    private lateinit var mData: PerfData

    private lateinit var mCoroutineScope: CoroutineScope

    private val mMainFloatWindow = MainFloatWindow(windowManager)
    private var mChartFloatWindow = ChartFloatWindow(windowManager)

    companion object {
        private const val UPDATE_INTERVAL = 1000L // 更新间隔，以毫秒为单位

        private const val TAG="FloatWindowManager"
    }

    fun addView() {
        mAbnormalDataRecordManage = AbnormalDataRecordManager()
        mMainFloatWindow.setRecordAbnormalListener(mAbnormalDataRecordManage)
        mMainFloatWindow.setChartFloatListener(mChartFloatWindow)
        mMainFloatWindow.addView()
    }

    //移除
    fun removeView() {
        mMainFloatWindow.removeView()
        mChartFloatWindow.removeView()
    }

    //移除数据拉取操作
    fun stopFetchData() {
        mCoroutineScope.cancel()//取消协程
    }

    //数据处理的操作
    fun fetchData() {
        mRecordManager = RecordManager()
        mMainFloatWindow.setRecordListener(mRecordManager)
        mMainFloatWindow.setStopServiceListener(service)

        mCoroutineScope = CoroutineScope(Dispatchers.IO)
        fetchDataFromBackgroundThread()//去拉取数据
    }


    private fun fetchDataFromBackgroundThread() {

        mCoroutineScope.launch(Dispatchers.IO) {
            while (isActive) {
                mData = mRecordManager.fetchData()//每一秒都会调用，频繁的创建销毁会占内存

                MemoryMonitor.updateMemoryCount(mData.memAvail)
                CpuMonitor.updateCpuCount(mData.sysCpuUsage)
                IOMonitor.updateCount(mData.inSpeed, mData.outSpeed)

                if (MainFloatWindow.isRecord) {//如果开始录制了，就需要把数据传递给录制管理类，保存数据
                    mRecordManager.setPerfData(mData)
                }
                if (MemoryMonitor.isAbnormal() || CpuMonitor.isAbnormal() || IOMonitor.isInAbnormal() || IOMonitor.isOutAbnormal()) {//任何一个数据有异常则记录到异常数据文件中
                    mAbnormalDataRecordManage.setData(
                        mData,
                        MemoryMonitor.getThresholdMemory(),
                        CpuMonitor.getThresholdCpu(),
                        IOMonitor.getThresholdIOIn(),
                        IOMonitor.getThresholdIOOut()
                    )
                    // 异常截图
                    val mediaProjectionManager =
                        App.mInstance.getSystemService(Service.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
                    App.mediaProjection = mediaProjectionManager.getMediaProjection(code, data)
                    mAbnormalDataRecordManage.captureScreen(resources)
                }

                withContext(Dispatchers.Main) {
                    mMainFloatWindow.updateUI(mData, resources)//刷新主窗口的UI以及，异常报警
                    mChartFloatWindow.updateUI(mData, resources)//折线图的数据入口，刷新折线图的实时数据
                }
                delay(UPDATE_INTERVAL)
            }
        }
    }


}